package com.pang.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.pang.springcloud.service.PaymentHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author： pang-yun
 * @date： 2022-02-12 14:50
 * Description:
 */
@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService hystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    @HystrixCommand
    public String paymentInfo_OK(@PathVariable("id") Long id) {
        int i =1/0;
        return hystrixService.paymentInfo_OK(id);
    }


    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "handler",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
            })
    public String paymentInfo_TIMEOUT(@PathVariable("id") Long id) {
        return hystrixService.paymentInfo_TIMEOUT(id);
    }

    // 服务降级后使用的方法
    public String handler(@PathVariable("id") Long id) {
        return "sorry  this is client hystrix service  !";
    }

    // 降级使用的方法名称可以不同 ， 但是参数必须相同 否则会报错
    public String noParamHandler() {
        return "this is no params";
    }

    public String payment_Global_FallbackMethod(){
        return "Global fallback method";
    }

}
